/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.swta;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

/**
 *
 * @author JKorreck
 */
public class ItemPackageActionPanel extends Panel {

    MyItemPackages parent;
    ConfirmDeleteItemPage deletePage = new ConfirmDeleteItemPage();
    SelectModalWindow deleteWindow;
    ItemPackageEditModalWindow editWindow;

    String selectedID = "";

    /**
     * @param id
     *            component id
     * @param model
     *            model for contact
     * @param parent
     */
    public ItemPackageActionPanel(String id, IModel model, MyItemPackages parent) {
        super(id, model);
        this.parent = parent;

        
        add(new Link("select") {

            public void onClick() {
//                    selected = (getParent()).getModelObject();
//                this.getParent().getDefaultModelObject();
                ItemPackageActionPanel.this.parent.setSelected((MyItemPackage) (this.getParent()).getDefaultModelObject());

            }
        });

        this.editWindow = new ItemPackageEditModalWindow("editWindow", this.parent, new MyItemPackage()) {

            @Override
            void onCancel(AjaxRequestTarget target) {

                close(target);
                ItemPackageActionPanel.this.selectedID = "";
            }

            @Override
            void onSelect(AjaxRequestTarget target, String selection) {
                close(target);
                ItemPackageActionPanel.this.selectedID = "";
            }
        };

        add(editWindow);

        add(new AjaxLink("editLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                add(ItemPackageActionPanel.this.editWindow);
                MyItemPackage sel = (MyItemPackage) (this.getParent()).getDefaultModelObject();

                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
                ItemPackageActionPanel.this.editWindow.setTextMessage("Edit Element " + sel.getTitle() + " (" + sel.getId() + ").");
                ItemPackageActionPanel.this.editWindow.setTitleText("Edit of "  + sel.getTitle());
                ItemPackageActionPanel.this.selectedID = sel.getId();

//                ItemPackageActionPanel.this.parent.editSelectedItem((MyItemPackage) (this.getParent()).getDefaultModelObject());
                ((ItemPackageEditContentPanel) ItemPackageActionPanel.this.editWindow.get(ItemPackageActionPanel.this.editWindow.getContentId())).setItemToEdit(sel.getId());
                ItemPackageActionPanel.this.editWindow.show(target);
            }
        });

        this.deleteWindow = new SelectModalWindow("modalwindow") {

            void onSelect(AjaxRequestTarget target, String selection) {
                // Handle Select action
//                resultLabel.setModelObject(selection);
//                target.addComponent(resultLabel);

                MyItemPackageManager.getInstance().removeItemPackage(ItemPackageActionPanel.this.selectedID);

                close(target);
                ItemPackageActionPanel.this.selectedID = "";
//                setResponsePage(MyItems.class);
            }

            void onCancel(AjaxRequestTarget target) {
                // Handle Cancel action
//                resultLabel.setModelObject("ModalWindow cancelled.");
//                target.addComponent(resultLabel);
                // nothing
                close(target);
                ItemPackageActionPanel.this.selectedID = "";
            }
        };

        add(deleteWindow);

        add(new AjaxLink("removeLink") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                add(ItemPackageActionPanel.this.deleteWindow);
                MyItemPackage sel = (MyItemPackage) (this.getParent()).getDefaultModelObject();

                target.appendJavascript("Wicket.Window.unloadConfirmation = false;");
                ItemPackageActionPanel.this.deleteWindow.setTextMessage("Soll das Element " + sel.getTitle() + " (" + sel.getId() + ") wirklich gelöscht werden?");
                ItemPackageActionPanel.this.deleteWindow.setTitleText("Löschen von " + sel.getTitle() + "?");
                ItemPackageActionPanel.this.selectedID = sel.getId();
                ItemPackageActionPanel.this.deleteWindow.show(target);
            }
        });
    }
}
